<?php
/* 
* @Title:  [分类控制器]
* @Author: {chenlei} [chenleib5@126.com]
* @Date:   2014-04-03 22:07:41
* @Last Modified by:   Administrator
* @Last Modified time: 2014-04-10 11:26:29
* @Copyright:  [hn7m.com]
*/
class CategoryControl extends CommonControl{
	private $model=NULL;

	/**
	 * [__init 自动加载模型类]
	 * @return [type] [description]
	 */
	public function __init(){
		parent::__init();
		$this->model=K('Category');
	}
	/**
	 * [index 默认显示分类列表]
	 * @return [type] [description]
	 */
	public function index(){
		$cid=Q('get.pid',0,'intval');
		$where=array('cpid'=>$cid);
		//执行模型方法
		$cate=$this->model->get_data($field=NULL,$where=NULL,$csort=NULL);
// p($cate);die();		
		// $cate= Data::tree($cate,'cname','cid','cpid');  //获得树状数据
		$cate = Data::channelList($cate, 0, '&nbsp&nbsp&nbsp|----', 'cid','cpid');    //获得目录列表
// p($cate);die();
		$where=array('cpid'=>0,'cshow'=>1);
		$csort='csort ASC';
		$field=array('cid','cname','cpid','cshow','csort');
		$topCate=$this->model->get_data($field,$where,$csort);   //获得所有顶级分类

		$this->assign('topCate',$topCate);
		$this->assign('cate',$cate);

		$this->display();
	}
	/**
	 * [add_cate 添加分类]
	 */
	public function add_cate(){
		if(IS_AJAX){
			if(!$this->model->create()){
				$return=array(
					'state'		=>0,
					'message'	=>$this->model->error,
					'timeout'	=>3
					);
				$this->ajax($return);
			}
			//执行模型添加方法
			if($this->model->add_data()){
				$return =array(
					'state'		=>1,
					'message'	=>'恭喜您,添加成功!',
					'timeout'	=>3
					);
				$this->ajax($return);
			}
		}
		//增加子级分类
		$field=array('cid','cname');
		$cid=Q('cid',0,'intval');
		$data=$this->model->get_one_data($cid,$field);
		//分配商品类型
		$type=K('Type')->get_data();
		//分配最大排序序号
		$maxSort=$this->model->get_max('csort',$cid);
		$this->assign('maxSort',$maxSort); 

		$this->assign('type',$type);
		$this->assign('data',$data);
		$this->display();
	}
	/**
	 * [edit_cate 修改分类]
	 * @return [type] [description]
	 */
	public function edit_cate(){
		$cid=Q('cid',0,'intval');
		$cate=K('Category')->get_one_data($cid); //获得原数据
		$this->assign('cate',$cate);			//分配原数据

		//分配所选分类
		$data = $this->model->get_chose_cate($cid);
		$data = Data::channelList($data, 0, '&nbsp&nbsp&nbsp|----->', 'cid','cpid');
		$this->assign('data', $data);
		//分配所选类型
		$type=K('Type')->get_data();
		$this->assign('type',$type);
		if(IS_AJAX){
			if(!$this->model->create()){
				$return=array(
					'state'		=>0,
					'message'	=>$this->model->error,
					'timeout'	=>3
					);
				$this->ajax($return);
			}
			if($this->model->edit_data($cid)){
				$return=array(
					'state'		=>1,
					'message'	=>'恭喜您,分类修改成功',
					'timeout'	=>3
					);
				$this->ajax($return);
			}
		}	
		$this->display();
	}
	/**
	 * [del_cate 删除分类,如果含有子分类,则要先删除子分类]
	 * @return [type] [description]
	 */
	public function del_cate(){
		if(IS_AJAX){
			$cid = Q('cid', 0, 'intval');
			if(!$this->model->have_data($cid)){
				$return = array(
					'state' => 0,
					'message' => $this->model->error,
					'timeout'=>3
				);
				$this->ajax($return);
			}

			//执行删除
			$this->model->del_data($cid);
			$return = array(
					'state' => 1,
					'message' => '删除成功',
					'timeout'=>3
				);
			$this->ajax($return);
		}
	}

}
?>